package com.food.restaurantpro.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.food.restaurantpro.R;
import com.food.restaurantpro.RestaurantApp;
import com.food.restaurantpro.fragments.FoodGridViewFragment;
import com.food.restaurantpro.imageloader.FileCache;
import com.food.restaurantpro.objects.Food;

public class FoodListViewAdapter extends ArrayAdapter<Food> {
	private Context mContext;
	private FileCache m_FileCache;
	public ArrayList<Food> mFoodList = new ArrayList<Food>();
	private LayoutInflater inflater;
	
	//Khi checkVisiblePreOrder > 0 thi visible RelativeLayout Pre-Order
	private RelativeLayout mRLPreOrder;


	public FoodListViewAdapter(Context context, ArrayList<Food> foodList, RelativeLayout rlPreOrder) {
		super(context, R.layout.food_lv_item, R.id.rowName, foodList);
		this.mContext = context;
		this.mFoodList = foodList;
		mRLPreOrder = rlPreOrder;
		m_FileCache = new FileCache(RestaurantApp.CURRENT_CONTEXT);
		inflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		return mFoodList.size();
	}

	@Override
	public Food getItem(int position) {
		return mFoodList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressWarnings("deprecation")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View listView = convertView;
		FoodHolder holder = null;
		if (convertView == null) {
			holder = new FoodHolder();
			listView = inflater.inflate(R.layout.food_lv_item, null);
			holder.tvName = (TextView) listView.findViewById(R.id.rowName);
			holder.tvContent = (TextView) listView.findViewById(R.id.rowContent);
			holder.tvPrice = (TextView) listView.findViewById(R.id.rowPrice);
			holder.imageView = (ImageView) listView.findViewById(R.id.imageview);
			holder.btnOrder = (Button) listView.findViewById(R.id.btnOrder);
			holder.mRLBackgroundListView = (RelativeLayout) listView.findViewById(R.id.rl_bg_listview);
			listView.setTag(holder);

		} else {
			listView = convertView;
			holder = (FoodHolder) listView.getTag();
		}
		final Food food = mFoodList.get(position);
		holder.tvName.setText(food.getFoodName());
		holder.tvContent.setText(food.getFoodDescription());
		holder.tvPrice.setText("$" + food.getFoodPrice());
		holder.imageView.setImageBitmap(m_FileCache.getBitmapByUrl(food.getFoodImage()));
		holder.imageView.setScaleType(ImageView.ScaleType.FIT_XY);
		//code o day va ben FoodGridViewAdapter phai luon giong nhau
		holder.btnOrder.setSelected(food.isSelected());
		
		if(food.isSelected()) {
//			holder.btnOrder.setBackgroundResource(R.drawable.menu_oder_selected);
			setSelected(holder);
		} else {
//			holder.btnOrder.setBackgroundResource(R.drawable.menu_oder_default);
			setDefault(holder);
		}
		
		holder.btnOrder.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Button b = (Button) v;
				if (b.isSelected()) {
					FoodGridViewFragment.checkVisiblePreOrder--;
					food.setIsSelected(false);
					b.setSelected(food.isSelected());
//					b.setBackgroundResource(R.drawable.menu_oder_default);
					notifyDataSetChanged();
				} else {
					FoodGridViewFragment.checkVisiblePreOrder++;
					food.setIsSelected(true);
					b.setSelected(food.isSelected());
//					b.setBackgroundResource(R.drawable.menu_oder_selected);
					notifyDataSetChanged();
				}
				if(FoodGridViewFragment.checkVisiblePreOrder > 0) {
					mRLPreOrder.setVisibility(View.VISIBLE);
				} else {
					mRLPreOrder.setVisibility(View.GONE);
				}
				/*for (int i = 0; i < mFoodList.size(); i++) {
					if (mFoodList.get(i).isSelected()) {
						mRLPreOrder.setVisibility(View.VISIBLE);
						return;
					} else {
						mRLPreOrder.setVisibility(View.GONE);
					}
				}*/
			}
		});
		return listView;
	}

	private void setSelected(FoodHolder holder) {
		holder.btnOrder.setBackgroundResource(R.drawable.menu_oder_selected);
		holder.tvName.setTextColor(mContext.getResources().getColor(R.color.color_white_text));
		holder.tvContent.setTextColor(mContext.getResources().getColor(R.color.color_white_text));
		holder.tvPrice.setTextColor(mContext.getResources().getColor(R.color.color_yellow_text));
		holder.mRLBackgroundListView.setBackgroundResource(R.drawable.menu_oder_lv_selected);
	}
	
	private void setDefault(FoodHolder holder) {
		holder.btnOrder.setBackgroundResource(R.drawable.menu_oder_default);
		holder.tvName.setTextColor(mContext.getResources().getColor(R.color.color_black_text));
		holder.tvContent.setTextColor(mContext.getResources().getColor(R.color.color_black_text));
		holder.tvPrice.setTextColor(mContext.getResources().getColor(R.color.color_red_text));
		holder.mRLBackgroundListView.setBackgroundResource(R.drawable.menu_oder_lv_default);
	}
	//
	private static class FoodHolder {
		public TextView tvName;
		public TextView tvContent;
		public TextView tvPrice;
		public ImageView imageView;
		public Button btnOrder;
		public RelativeLayout mRLBackgroundListView;
	}
	
	public void clearMemoryCache() {
		if (mFoodList != null) {
			mFoodList.clear();
			mFoodList = null;
		}
		m_FileCache = null;
		inflater = null;
	}
}
